<?php
class SEF {
	public static $url_decode;
	public static $url_request;
	public static $url;
	public static $arr;
	/** массив со значениями перевода кириллицы в латиницу */
	private static $cirtolat = array (
		'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Ye', 'Ж' => 'Zh', 
		'З' => 'Z', 'И' => 'I', 'Й' => 'Yi', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 
		'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 
		'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Shch', 'Ь' => '', 'Ы' => 'Y', 
		'Ъ' => '', 'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya', 
		// --
		'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'ye', 'ж' => 'zh', 
		'з' => 'z', 'и' => 'i', 'й' => 'yi', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 
		'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 
		'x' => 'h', 'ц' => 'c', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'shch', 'ь' => '', 'ы' => 'y', 
		'ъ' => '', 'э' => 'e', 'ю' => 'yu', 'я' => 'ya', 
		// --
		'á' => 'a', 'ä' => 'a', 'ć' => 'c', 'č' => 'c', 'ď' => 'd', 'é' => 'e', 'ě' => 'e', 'ë' => 'e', 'í' => 'i', 'ň' => 'n', 'ń' => 'n', 'ó' => 'o', 'ö' => 'o', 'ŕ' => 'r', 'ř' => 'r', 'š' => 's', 'Š' => 'S', 'ť' => 't', 'ú' => 'u', 'ů' => 'u', 'ü' => 'u', 'ý' => 'y', 'ź' => 'z', 'ž' => 'z', 'і' => 'i', 'ї' => 'i', 'b' => 'b', 'І' => 'i', // --
		' ' => '_', '\'' => '', '"' => '' 
	);
	private static function MakeArray($url) {
		$tExp = explode('=', $url);
		$tExp[0] = trim($tExp[0]);
		$tExp[1] = trim($tExp[1]);
		if ($tExp[1] != '') {
			if (strpos($tExp[1], ';') !== FALSE) {
				$subArr = explode(';', $tExp[1]);
				for($i = 0; isset($subArr[$i]); ++$i) {
					if ($subArr[$i] != '') {
						self::$arr[$tExp[0]][$i] = $subArr[$i];
					}
				}
			} else {self::$arr[$tExp[0]] = $tExp[1];}
		}
	}
	public static function makeURL() {
		// список основных ссылок
		$firstArr = array ();
		$bFirst   = FALSE; 
		$bURL     = TRUE;
		if (file_exists (PAGES_PATH.'/pages.list.php')) {
			include PAGES_PATH.'/pages.list.php';
		}
		self::$arr = array ();
		if (isset($_SERVER['REQUEST_URI'])) {
			self::$url_request = $_SERVER['REQUEST_URI'];
			self::$url_decode  = self::Decode(self::$url_request);
		} else {self::$url_request = self::$url_decode = '';}
		self::$url = '';
		// разбивка на массив по '/'
		$mainArr = explode('/', self::$url_decode);
		for($i = 0, $u = 0; isset($mainArr[$i]); ++$i) {
			$main = trim($mainArr[$i]);
			if ($main == '') {continue;}
			if (strpos($main, '~') !== FALSE) {
				// разбивка на массив по '~'
				$subArr = explode('~', $main);
				for($j = 0; isset($subArr[$j]); ++$j) {
					if ($u == 0) {
						foreach($firstArr as $url) {
							if ($url == $subArr[0]) {
								$bFirst = TRUE;
								break;
							}
						}
					}
					if ($j == 0) {
						$subArr[0] = trim($subArr[0]);
						self::$arr[$u++] = $subArr[0];
						if ($bURL) {self::$url .= '/'.$subArr[0];}
					} else {self::MakeArray($subArr[$j]);}
					if ($bFirst) {$bFirst = $bURL = FALSE;}
				}
			} else {
				if ($u == 0) {
					foreach ($firstArr as $url) {
						if ($url == $main) {
							$bFirst = TRUE;
							break;
						}
					}
				}
				self::$arr[$u++] = $main;
				if ($bURL) {self::$url .= '/'.$main;}
				if ($bFirst) {$bFirst = $bURL = FALSE;}
			}
		}
		if (self::$url == '') {self::$url = '/main';}
		return 0;
	}
	static function Encode($URL) {
		return urlencode ( strtr ( $URL, self::$cirtolat ) );
	}
	static function Decode($URL) {
		return urldecode ( $URL );
	}
}
?>
